package com.library.rest.rxjava;

import com.library.rest.vo.ResponseModel;
import rx.Observable;
import rx.functions.Func1;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-07-01 21:16
 * @version: 9.1.0
 */
public class RxResultHelper {
    public static <T> Observable.Transformer<ResponseModel<T>, T> handleResult() {
        return new Observable.Transformer<ResponseModel<T>, T>() {
            @Override
            public Observable<T> call(Observable<ResponseModel<T>> tObservable) {
                return tObservable.flatMap(
                        new Func1<ResponseModel<T>, Observable<T>>() {
                            @Override
                            public Observable<T> call(ResponseModel<T> result) {
                                if (result.isSuccess()) {
                                    return Observable.just(result.data);
                                } else {
                                    return Observable.error(new ServerException(result.code, result.message));
                                }
                            }
                        }
                );
            }
        };
    }
}
